Ext.namespace('ZZExt.widgets');

ZZExt.widgets.CTreeCombo=Ext.extend(Ext.form.ComboBox,{
	tree:null,
	singleSelected:false,
	enableTbar:true,
	treeDataUrl:'systemMenu!systemMenuParent.action',
	treeRootText:'所有栏目',
	editable:false,
	
	constructor:function(config){
	  Ext.apply(this,config);
	  this.comboBoxID=new Date().getTime();
	  var _root=new Ext.tree.AsyncTreeNode({
		   loader:new Ext.tree.TreeLoader({
				 dataUrl:this.treeDataUrl
				
		   }),
		   expanded:true,
		   id:'root',
		   text:this.treeRootText,
		   checked:false,
		   listeners:{
		         'expand':function(p){ this.setChecked(this.tree.getRootNode(),this.getValue());},
                  scope:this
	        }
		});
	  if(this.singleSelected)_root.attributes.checked=null;
	  this.tree=new Ext.tree.TreePanel({
		   border:false,
		   root:_root,
		   tbar:new Ext.Toolbar(),   
           listeners:{
           		scope:this,
		         'beforeappend' : function( tree, current,node ){
	                  if(!this.singleSelected)node.attributes.checked=false;
	                  else node.attributes.checked=null;
	                  return true;
	              },
	              'checkchange': function(node,checked){
	            	  if(node.hasChildNodes()){
	            		  if(checked)node.expand();
	            		  for(var i=0;i<node.childNodes.length;i++){
	            			  var cNode=node.childNodes[i];
	            			  if(checked)
	            			     cNode.getUI().toggleCheck(true);
	            			  else cNode.getUI().toggleCheck(false);
	            		  }
	            	  }
	            	 
	              }
	              
	         }
		   
	  });
	 if(this.enableTbar){
		 var tbar=this.tree.getTopToolbar();
		 tbar.add('请选择','-','->');
	     tbar.add({text:'确定',handler:this.saveSelected,scope:this},'-');
	     tbar.add({text:'取消',handler:this.cancelSelected,scope:this});
	 }
		
	  ZZExt.widgets.CTreeCombo.superclass.constructor.call(this,{
		  mode:'local',
		  triggerAction:'all',
		  readOnly:true,
		  store:new Ext.data.SimpleStore({fields:[],data:[[]]}),
		  tpl: '<tpl for="."><div id="treepanel'+this.comboBoxID+'" style="visibility:visible;height:180px;"></div></tpl>',
		  listeners:{
		     'expand':function(com){
		                
		                this.tree.render('treepanel'+this.comboBoxID);
		                this.tree.expandAll();
		               
	               },scope:this
	      }
		 
	  });
	  
	},
   getCheckedNodeValue:function(node){
	   //只记录叶子
		if((node.isLeaf()||node.id=='root')&&node.getUI().isChecked())
			return node.id;
		    var val='';
		    var flag=false;
		     for(var i=0;i<node.childNodes.length;i++){
		     	 
			     var cNode=node.childNodes[i];
			     var t=this.getCheckedNodeValue(cNode);
		         if(t!=null&&t!=''){
		         	flag=true;
		         	val+=t+',';
		         }
		     }
		if(flag&&node.id!='root')val=node.id+','+val;
		return val==''?'':val.substring(0,val.length-1);

	},
	setChecked:function(node,val){
		if(val=='root')node.getUI().toggleCheck(true);
		if(node.isLeaf()){
			if(val.indexOf(node.id)>-1)
				node.getUI().toggleCheck(true);
			else 
				node.getUI().toggleCheck(false);
			return ;
		}
		node.expand();
		for(var i=0;i<node.childNodes.length;i++){
			 this.setChecked(node.childNodes[i],val);
		}
		
			
	},
   saveSelected:function(){
	   var val='';
	   var rNode=this.tree.getRootNode();
	   val=this.getCheckedNodeValue(rNode);
	   this.setValue(val);
	   this.collapse();
   },
   cancelSelected:function(){
	   this.setChecked(this.tree.getRootNode(),this.getValue());
	   this.collapse();
   }
	
});

Ext.reg('ctreeCombo',ZZExt.widgets.CTreeCombo);


